package com.abdulaziz.ms.ocv.editor;

import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.GEF;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.MultiPageEditorPart;

import com.abdulaziz.ms.OCV.OCVFactory;
import com.abdulaziz.ms.OCV.OCVPackage;
import com.abdulaziz.ms.OCV.UMLClassDiagram;
import com.abdulaziz.ms.ocv.gef.editor.OCVGraphicalEditor;

public class OCVMutiPageEditor extends MultiPageEditorPart implements
		IResourceChangeListener {
	private UMLClassDiagram umlClassDiagram;

	public OCVMutiPageEditor() {
		super();
		ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
	}

	@Override
	protected void createPages() {
		createMainPage();

	}

	private void createMainPage() {
		try {

			// create a model class first 
			loadInput(this.getEditorInput());
			//GEF editor 
			OCVGraphicalEditor gefEditor = new OCVGraphicalEditor(
					umlClassDiagram);
			int index = addPage(gefEditor, getEditorInput());
			setPageText(index, "Class Diagram Edior");
		} catch (PartInitException e) {
			ErrorDialog.openError(getSite().getShell(),
					"Error in creating GEF editor", null, e.getStatus());
		}

	}
	
	
	/*
	void createPage1() {

		
		Composite composite = new Composite(getContainer(), SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		layout.numColumns = 2;

		Button fontButton = new Button(composite, SWT.NONE);
		GridData gd = new GridData(GridData.BEGINNING);
		gd.horizontalSpan = 2;
		fontButton.setLayoutData(gd);
		fontButton.setText("click");

		fontButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				createVContractPage();
			}
		});

		int index = addPage(composite);
		setPageText(index, "Class Diagram");
	}
	*/

	private void loadInput(IEditorInput input) {
		if (umlClassDiagram == null) {
			OCVPackage.eINSTANCE.eClass();
			umlClassDiagram = OCVFactory.eINSTANCE.createUMLClassDiagram();
		}

	}

	public void createVContractPage() {
		
		
		try {
			OCVGraphicalEditor gefEditor = new OCVGraphicalEditor();
			int index = addPage(gefEditor, getEditorInput());
			setPageText(index, "VContract" + index);
			this.setActivePage(index);
		} catch (PartInitException e) {
			ErrorDialog.openError(getSite().getShell(),
					"Error in creating VContract editor", null, e.getStatus());
		}

	}

	@Override
	public void resourceChanged(IResourceChangeEvent event) {
		// TODO Auto-generated method stub

	}

	

	

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void dispose() {
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
		
		super.dispose();
	}

}
